package com.kz.controller;


import com.kz.entity.YcreBig;
import com.kz.service.YcreBigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zk
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/ycre-big")
public class YcreBigController {

    @Resource
    private YcreBigService ycreBigService;

    @PostMapping(value = "getYcreBigs")
    public List<YcreBig> getYcreBigs() {
        List<YcreBig> ycreBigs = ycreBigService.getYcreBigs();
        return ycreBigs;
    }

    @PostMapping(value = "getYcreBig")
    public List<YcreBig> getYcreBig(@RequestBody Map<String, String> map) {

        return ycreBigService.selectYcreBigById(map.get("accId") == null ? "" : map.get("accId"), map.get("id") == null ? 0 : Integer.parseInt(map.get("id")));
    }

    @PostMapping(value = "getYcreBigSS")
    public YcreBig getYcreBigSS(@RequestBody Map<String, String> map) {

        return ycreBigService.selectYcreBigById(map.get("accId") == null ? "" : map.get("accId"), map.get("id") == null ? 0 : Integer.parseInt(map.get("id"))).get(0);
    }

    @PostMapping(value = "saveOrUpdate")
    public String saveOrUpdate(@RequestBody YcreBig ycreBig) {
        try {
            if (ycreBig.getId() != null) {
                ycreBig.setCostList(null);
                ycreBigService.updateById(ycreBig);

                return "修改成功";
            } else {
                List<YcreBig> bigById = ycreBigService.selectYcreBigById(ycreBig.getLocationId(), ycreBig.getId());
                if (bigById == null || bigById.isEmpty()) {
                    ycreBig.setCreateTime(LocalDateTime.now());
                    ycreBig.setDeleted("N");

                    boolean insert = ycreBigService.insert(ycreBig);

                    if (insert) {
                        return "保存成功";
                    } else {
                        return "保存失败";
                    }
                } else {
                    return "位置号已存在!";
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
    }

    @PostMapping(value = "deleted")
    public String deleted(@RequestBody Map<String, String> map) {

        YcreBig ycreBig = ycreBigService.selectYcreBigById(map.get("id"), null).get(0);

        if (ycreBig != null) {
            ycreBig.setDeleted("Y");
            ycreBig.setCostList(null);
            ycreBigService.updateById(ycreBig);
            return "删除成功！！";
        }

        return "删除失败！！";
    }

}
